# Maintainer: Paul Bredbury <brebs@sent.com>
pkgname=swi-prolog
pkgver=8.5.13
pkgrel=0
pkgdesc="Versatile implementation of the Prolog programming language"
url="https://www.swi-prolog.org"
arch="all !riscv64" # missing libunwind-dev
license="BSD-2-Clause"
# Need to package JavaHamcrest in order to enable JPL
subpackages="$pkgname-doc $pkgname-xpce:xpce:noarch $pkgname-xpce-doc:xpce_doc:noarch"
makedepends="cmake
	gmp-dev
	libarchive-dev
	libedit-dev
	libunwind-dev
	libxext-dev
	libice-dev
	libjpeg-turbo-dev
	libxinerama-dev
	libxft-dev
	libxpm-dev
	libxt-dev
	ncurses-dev
	openssl-dev
	ossp-uuid-dev
	pcre2-dev
	readline-dev
	samurai
	unixodbc-dev
	yaml-dev
	zlib-dev"
# The Berkeley DB test works on all but x86, which segfaults
# https://github.com/SWI-Prolog/issues/issues/103#issuecomment-968091401
[ ! "$CARCH" = "x86" ] && makedepends="$makedepends db-dev"

checkdepends="python3"
source="https://www.swi-prolog.org/download/devel/src/swipl-$pkgver.tar.gz"
builddir="$srcdir/swipl-$pkgver"

build() {
	cmake -B "$builddir" -G Ninja \
		-DCMAKE_BUILD_TYPE=PGO \
		-DCMAKE_INSTALL_PREFIX=/usr
	ninja -C "$builddir"
}

check() {
	ctest --output-on-failure
}

package() {
	DESTDIR="$pkgdir" ninja -C "$builddir" install
}

doc() {
	default_doc
	mkdir -p "$subpkgdir"/usr/share/swi-prolog
	mv "$pkgdir"/usr/lib/swipl/doc "$subpkgdir"/usr/share/swi-prolog
}

xpce() {
	depends="$pkgname"
	pkgdesc="X support for $pkgname"
	mkdir -p "$subpkgdir"/usr/lib/swipl
	mv "$pkgdir"/usr/lib/swipl/xpce "$subpkgdir"/usr/lib/swipl

	# Move doc files
	mkdir -p "$subpkgdir"-doc/usr/lib/swipl/xpce
	mv "$subpkgdir"/usr/lib/swipl/xpce/appl-help "$subpkgdir"-doc/usr/lib/swipl/xpce
	mv "$subpkgdir"/usr/lib/swipl/xpce/man "$subpkgdir"-doc/usr/lib/swipl/xpce
}

xpce_doc() {
	pkgdesc="X support for $pkgname (doc files)"
}

sha512sums="
910a1cf27edc0e5fedad36cd9e16f37ed1fd35682c686b3ffda556104170a2ae14dbfc4d2b39e2c58bffaa8398fb81fc83058c3568260b7e9799b4910097fc3a  swipl-8.5.13.tar.gz
"
